'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Windows.Forms

Public Class iSage
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String

            'login 
            Dim POSTData As String = "user_name=" & ConfigurationData.Username & "&password=" & ConfigurationData.Password & "&submit=Login"
            ISPPost("https://www.isage.net.au/members/index.php", POSTData, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            strHTMLData = ISPGet("https://www.isage.net.au/members/index.php", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If strHTMLData.Contains("Enter your account number and password") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            'services tab
            strHTMLData = ISPGet("https://www.isage.net.au/members/index.php?cmd=services", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            'look for u_uid
            Dim u_uid As String = Trim(ExtractText(strHTMLData, "&u_uid", 0, "=", """"))

            If u_uid = "" Then
                ISPReturnData.ErrorMessage = "Error parsing website"
                Exit Sub
            End If

            Dim StartDate As Date = Date.ParseExact(ConfigurationData.RolloverDay & "/" & Now.Month & "/" & Now.Year, "d/M/yyyy", Nothing)
            If StartDate > Now Then StartDate = StartDate.AddMonths(-1)

            'usage
            POSTData = "cmd=services&u_uid=" & u_uid & "&mode=dsl_usage&chk_usage=yes&f_day=" & Format(StartDate.Day, "00") & "&f_month=" & Format(StartDate.Month, "00") & "&f_year=" & StartDate.Year & "&t_day=" & Format(Now.Day, "00") & "&t_month=" & Format(Now.Month, "00") & "&t_year=" & Now.Year & "&report_type=summary&Submit=Submit"
            strHTMLData = ISPPost("https://www.isage.net.au/members/index.php", POSTData, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            'logout
            ISPGet("https://www.isage.net.au/members/logout.php", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            '##################################################################################

            'process response
            ISPReturnData.PeakDownload = CSng(Val(Replace(ExtractText(strHTMLData, "Peak Incoming Data:", 0, "> ", " "), ",", "")))
            ISPReturnData.PeakUpload = CSng(Val(Replace(ExtractText(strHTMLData, "Peak Outgoing Data:", 0, "> ", " "), ",", "")))

            If ConfigurationData.OffpeakAvailable = True Then
                ISPReturnData.OffpeakDownload = CSng(Val(Replace(ExtractText(strHTMLData, "Off-Peak Incoming:", 0, "> ", " "), ",", "")))
                ISPReturnData.OffpeakUpload = CSng(Val(Replace(ExtractText(strHTMLData, "Off-Peak Outgoing:", 0, "> ", " "), ",", "")))
            End If

            ISPReturnData.LastUpdated = Now

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data
            If ConfigurationData.OffpeakAvailable = True Then
                'peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))

                'offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak " & Format(System.Math.Floor((ISPReturnData.OffpeakDownload / Val(ConfigurationData.OffpeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, CSng(Val(ConfigurationData.OffpeakUsageLimit)), ISPReturnData.OffpeakNow))
            Else
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))
            End If

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data
            ISPReturnData.TextItemList.Add(New TextItem("Usage Left :", ISPReturnData.PeakUsageRemainingDescription))
            ISPReturnData.TextItemList.Add(New TextItem("Usage Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))

            ISPReturnData.TextItemList.Add(New TextItem("Downloads :", ISPReturnData.PeakDownload & " MB"))
            ISPReturnData.TextItemList.Add(New TextItem("Uploads :", ISPReturnData.PeakUpload & " MB"))

            If ISPReturnData.ShapedNow = False Then
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
            End If

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try

    End Sub

End Class
